﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.MountAndBlade.View.PopupSceneSpawnPoint
// Assembly: TaleWorlds.MountAndBlade.View, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 61152EB8-513B-4D4A-8AC0-0D1465C6CF6C
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\Native\bin\Win64_Shipping_Client\TaleWorlds.MountAndBlade.View.dll

using TaleWorlds.Core;
using TaleWorlds.DotNet;
using TaleWorlds.Engine;
using TaleWorlds.Library;
using TaleWorlds.MountAndBlade.View.Scripts;

#nullable disable
namespace TaleWorlds.MountAndBlade.View
{
  public class PopupSceneSpawnPoint : ScriptComponentBehavior
  {
    public string InitialAction = "";
    public string NegativeAction = "";
    public string InitialFaceAnimCode = "";
    public string PositiveFaceAnimCode = "";
    public string NegativeFaceAnimCode = "";
    public string PositiveAction = "";
    public string LeftHandWieldedItem = "";
    public string RightHandWieldedItem = "";
    public string BannerTagToUseForAddedPrefab = "";
    public bool StartWithRandomProgress;
    public Vec3 AttachedPrefabOffset = Vec3.Zero;
    public string PrefabItem = "";
    public HumanBone PrefabBone = HumanBone.ItemR;
    private AgentVisuals _mountAgentVisuals;
    private AgentVisuals _humanAgentVisuals;
    private ActionIndexCache _initialStateActionCode;
    private ActionIndexCache _positiveStateActionCode;
    private ActionIndexCache _negativeStateActionCode;
    private static readonly ActionIndexCache default_act_horse_stand = ActionIndexCache.Create("act_horse_stand_1");
    private static readonly ActionIndexCache default_act_camel_stand = ActionIndexCache.Create("act_camel_stand_1");

    public CompositeComponent AddedPrefabComponent { get; private set; }

    protected override void OnInit()
    {
      base.OnInit();
      this.SetScriptComponentToTick(this.GetTickRequirement());
    }

    public void InitializeWithAgentVisuals(AgentVisuals humanVisuals, AgentVisuals mountVisuals = null)
    {
      this._humanAgentVisuals = humanVisuals;
      this._mountAgentVisuals = mountVisuals;
      this._initialStateActionCode = ActionIndexCache.Create(this.InitialAction);
      this._positiveStateActionCode = this.PositiveAction == "" ? this._initialStateActionCode : ActionIndexCache.Create(this.PositiveAction);
      this._negativeStateActionCode = this.NegativeAction == "" ? this._initialStateActionCode : ActionIndexCache.Create(this.NegativeAction);
      bool flag1 = !string.IsNullOrEmpty(this.RightHandWieldedItem);
      bool flag2 = !string.IsNullOrEmpty(this.LeftHandWieldedItem);
      if (flag2 | flag1)
      {
        AgentVisualsData agentVisualsData = this._humanAgentVisuals.GetCopyAgentVisualsData();
        Equipment equipment = this._humanAgentVisuals.GetEquipment().Clone();
        if (flag1)
          equipment[EquipmentIndex.WeaponItemBeginSlot] = new EquipmentElement(Game.Current.ObjectManager.GetObject<ItemObject>(this.RightHandWieldedItem));
        if (flag2)
          equipment[EquipmentIndex.Weapon1] = new EquipmentElement(Game.Current.ObjectManager.GetObject<ItemObject>(this.LeftHandWieldedItem));
        int rightWieldedItemIndex = flag1 ? 0 : -1;
        int leftWieldedItemIndex = flag2 ? 1 : -1;
        agentVisualsData.RightWieldedItemIndex(rightWieldedItemIndex).LeftWieldedItemIndex(leftWieldedItemIndex).Equipment(equipment);
        this._humanAgentVisuals.Refresh(false, agentVisualsData, false);
      }
      else if (!flag2 && !flag2)
      {
        AgentVisualsData agentVisualsData = this._humanAgentVisuals.GetCopyAgentVisualsData();
        Equipment equipment = this._humanAgentVisuals.GetEquipment().Clone();
        agentVisualsData.RightWieldedItemIndex(-1).LeftWieldedItemIndex(-1).Equipment(equipment);
        this._humanAgentVisuals.Refresh(false, agentVisualsData, false);
      }
      if (this.PrefabItem != "")
      {
        if (!GameEntity.PrefabExists(this.PrefabItem))
        {
          MBDebug.ShowWarning("Cannot find prefab '" + this.PrefabItem + "' for popup agent '" + this.GameEntity.Name + "'");
        }
        else
        {
          this.AddedPrefabComponent = this._humanAgentVisuals.AddPrefabToAgentVisualBoneByBoneType(this.PrefabItem, this.PrefabBone);
          if ((NativeObject) this.AddedPrefabComponent != (NativeObject) null)
            this.AddedPrefabComponent.Frame = MatrixFrame.Identity with
            {
              origin = this.AttachedPrefabOffset
            } * this.AddedPrefabComponent.Frame;
        }
      }
      foreach (GameEntity gameEntity in this.GameEntity.Scene.FindEntitiesWithTag(this.GameEntity.Name))
        gameEntity.GetFirstScriptOfType<PopupSceneSequence>()?.InitializeWithAgentVisuals(humanVisuals);
      this._humanAgentVisuals?.GetVisuals()?.CheckResources(true);
      this._mountAgentVisuals?.GetVisuals()?.CheckResources(true);
      this._mountAgentVisuals?.Tick((AgentVisuals) null, 0.0001f);
      this._mountAgentVisuals?.GetEntity()?.Skeleton?.ForceUpdateBoneFrames();
      this._humanAgentVisuals?.Tick(this._mountAgentVisuals, 0.0001f);
      this._humanAgentVisuals?.GetEntity()?.Skeleton?.ForceUpdateBoneFrames();
    }

    public void SetInitialState()
    {
      if (this._initialStateActionCode != (ActionIndexCache) null)
      {
        float startProgress = this.StartWithRandomProgress ? MBRandom.RandomFloatRanged(0.5f) : 0.0f;
        this._humanAgentVisuals?.SetAction(this._initialStateActionCode, startProgress, true);
        this._mountAgentVisuals?.SetAction(this._initialStateActionCode, startProgress, true);
      }
      if (!string.IsNullOrEmpty(this.InitialFaceAnimCode))
        this._humanAgentVisuals.GetVisuals().GetSkeleton().SetFacialAnimation(Agent.FacialAnimChannel.Mid, this.InitialFaceAnimCode, false, true);
      foreach (GameEntity gameEntity in this.GameEntity.Scene.FindEntitiesWithTag(this.GameEntity.Name))
        gameEntity.GetFirstScriptOfType<PopupSceneSequence>()?.SetInitialState();
    }

    public void SetPositiveState()
    {
      if (this._positiveStateActionCode != (ActionIndexCache) null)
      {
        this._humanAgentVisuals?.SetAction(this._positiveStateActionCode, 0.0f, true);
        this._mountAgentVisuals?.SetAction(this._positiveStateActionCode, 0.0f, true);
      }
      if (!string.IsNullOrEmpty(this.PositiveFaceAnimCode))
        this._humanAgentVisuals.GetVisuals().GetSkeleton().SetFacialAnimation(Agent.FacialAnimChannel.Mid, this.PositiveFaceAnimCode, false, true);
      foreach (GameEntity gameEntity in this.GameEntity.Scene.FindEntitiesWithTag(this.GameEntity.Name))
        gameEntity.GetFirstScriptOfType<PopupSceneSequence>()?.SetPositiveState();
    }

    public void SetNegativeState()
    {
      if (this._negativeStateActionCode != (ActionIndexCache) null)
      {
        this._humanAgentVisuals?.SetAction(this._negativeStateActionCode, 0.0f, true);
        this._mountAgentVisuals?.SetAction(this._negativeStateActionCode, 0.0f, true);
      }
      if (!string.IsNullOrEmpty(this.NegativeFaceAnimCode))
        this._humanAgentVisuals.GetVisuals().GetSkeleton().SetFacialAnimation(Agent.FacialAnimChannel.Mid, this.NegativeFaceAnimCode, false, true);
      foreach (GameEntity gameEntity in this.GameEntity.Scene.FindEntitiesWithTag(this.GameEntity.Name))
        gameEntity.GetFirstScriptOfType<PopupSceneSequence>()?.SetNegativeState();
    }

    public void Destroy()
    {
      this._humanAgentVisuals?.Reset();
      this._humanAgentVisuals = (AgentVisuals) null;
      this._mountAgentVisuals?.Reset();
      this._mountAgentVisuals = (AgentVisuals) null;
      this._initialStateActionCode = (ActionIndexCache) null;
      this._positiveStateActionCode = (ActionIndexCache) null;
      this._negativeStateActionCode = (ActionIndexCache) null;
    }

    public override ScriptComponentBehavior.TickRequirement GetTickRequirement()
    {
      return ScriptComponentBehavior.TickRequirement.Tick | base.GetTickRequirement();
    }

    protected override void OnTick(float dt)
    {
      this._mountAgentVisuals?.Tick((AgentVisuals) null, dt);
      this._mountAgentVisuals?.GetEntity()?.Skeleton?.ForceUpdateBoneFrames();
      this._humanAgentVisuals?.Tick(this._mountAgentVisuals, dt);
      this._humanAgentVisuals?.GetEntity()?.Skeleton?.ForceUpdateBoneFrames();
    }
  }
}
